class User < ActiveRecord::Base

  hobo_model
  
  has_many :questions
  has_many :answers
  has_many :completions
  has_many :questionnaires, :through => :completions

  include Hobo::AuthenticatedUser
  
  set_login_attr :username
  
  alias_attribute :display_name, :username
  
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_presence_of :address
  validates_presence_of :city 
  validates_presence_of :state
  validates_presence_of :zip
  validates_presence_of :email

  # --- Hobo Permissions --- #

  def super_user?
    login == 'admin' || administrator?
  end

  def creatable_by?(creator)
    creator.guest?
  end

  def updatable_by?(updater, new)
    updater == self
  end

  def deletable_by?(deleter)
    deleter == self
  end

  def viewable_by?(viewer, field)
    viewer == self 
  end


  # --- Fallback permissions --- #

  # (Hobo checks these for models that do not define the *_by? methods)

  def can_create?(obj)
    false
  end

  def can_update?(obj, new)
    false
  end

  def can_delete?(obj)
    false
  end

  def can_view?(obj, field)
    true
  end

  def guest?
    false
  end

end
